package com.fandou.base.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.blankj.utilcode.util.ScreenUtils;
import com.fandou.base.R;

/**
 * @author zdxiang
 * @date 2019/6/12 0:22
 */
public class TipConfirmDialog extends AlertDialog implements View.OnClickListener, LifecycleObserver {

    private TextView tvTitle;
    private TextView tvMessage;
    private TextView tvCancel;
    private TextView tvConfirm;
    private View v_line;

    private String mTitle;
    private CharSequence mMessage;
    private String mCancelText = "取消";
    private String mConfirmText = "确定";

    private boolean mShowCancelButton = false;

    public TipConfirmDialog(@NonNull Context context) {
        super(context);
        init(context);
    }


    private void init(Context context) {
        if (context instanceof AppCompatActivity) {
            ((AppCompatActivity) context).getLifecycle().addObserver(this);
        }
        setCanceledOnTouchOutside(false);
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_tip_confirm, null);
        setView(inflate);
        tvTitle = inflate.findViewById(R.id.tv_title);
        tvMessage = inflate.findViewById(R.id.tv_message);
        tvCancel = inflate.findViewById(R.id.tv_cancel);
        tvConfirm = inflate.findViewById(R.id.tv_confirm);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        v_line = inflate.findViewById(R.id.v_line);
    }

    public TipConfirmDialog setTitle(String title) {
        mTitle = title;
        return this;
    }

    public TipConfirmDialog setDialogMessage(CharSequence message) {
        mMessage = message;
        return this;
    }


    public TipConfirmDialog setCancelText(String cancelText) {
        mCancelText = cancelText;
        return this;
    }

    public TipConfirmDialog setConfirmText(String confirmText) {
        mConfirmText = confirmText;
        return this;
    }

    public TipConfirmDialog build() {
        tvTitle.setText(mTitle);
        tvMessage.setText(mMessage);
        tvCancel.setText(mCancelText);
        tvConfirm.setText(mConfirmText);
        if (!TextUtils.isEmpty(mCancelText)) {
            tvCancel.setVisibility(View.VISIBLE);
            v_line.setVisibility(View.VISIBLE);
        } else {
            tvCancel.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(mTitle)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
        }
        return this;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancel) {
            dismiss();
            if (mOnCancelListener != null) {
                mOnCancelListener.onCancel();
            }
        } else if (v.getId() == R.id.tv_confirm) {
            dismiss();
            if (onConfirmListener != null) {
                onConfirmListener.onConfirm();
            }
        }

    }

    private OnConfirmListener onConfirmListener;

    public TipConfirmDialog setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
        return this;
    }

    public interface OnConfirmListener {
        void onConfirm();
    }


    private OnCancelListener mOnCancelListener;

    public TipConfirmDialog setmOnCancelListener(OnCancelListener mOnCancelListener) {
        this.mOnCancelListener = mOnCancelListener;
        return this;
    }

    public interface OnCancelListener {
        void onCancel();
    }

    public TipConfirmDialog showDialog() {
        this.show();
        return this;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    @Override
    public void dismiss() {
        super.dismiss();
    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ScreenUtils.getScreenWidth() / 2;
            window.setAttributes(lp);
        }
    }
}
